javascript 您所在的位置:网站首页 cmts 金融 javascript

javascript

2023-08-24 15:01| 来源: 网络整理| 查看: 265

我在使用Vue.js做一个评论列表的demo的时候,将输入框输入的用户名和评论内容存储在localStorage中并通过列表展示,我想通过点击实现删除对应的评论,同时更新localStorage中的数据。

这是子组件中存储数组的方法:

postComment(){ var comment = {id:Math.random(),user:this.user, content:this.content}; // 从localStorage中获取所有的评论 var list = JSON.parse(localStorage.getItem('cmts')||'[]'); list.unshift(comment) // 保存最新的评论数据 localStorage.setItem('cmts',JSON.stringify(list)) this.user = this.content = '' this.$emit('func') }

删除评论的方法:

del(id){ this.list.some((item,i)=>{ var list = JSON.parse(localStorage.getItem('cmts')||'[]'); if(item.id === id){ this.list.splice(i,1); return true; } // 将删除对应数据后新的评论列表存到list中 localStorage.removeItem('cmts',JSON.stringify(list)); }) }

这是存储在localStorage中的数组:

使用removeItem()会把整个list数组给删除了:

localStorage.removeItem('cmts',JSON.stringify(list));

尝试过在使用some方法删除评论后,重新将list数组存储到localStorage当中去,但list数组没更新。 我想知道怎么通过id值删除localStorage中存储的list数组的对应对象。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有